import { performScriptForEval } from './performScript'
import { SnapShotSandbox } from './snapShotSandbox'
import { ProxySandbox } from './proxySandbox'

const isCheckLifecycle = (lifecycle) => {
    return lifecycle && lifecycle.bootstrap && lifecycle.mounted && lifecycle.unmount
}
export const sandbox = (app, script) => {
    const proxy = new ProxySandbox()

    if (!app.proxy) {
        app.proxy = proxy
    }
    // 1. 设置环境变量
    window.__MICRO_WEB__ = true
    // 运行js文件
    const lifecycle = performScriptForEval(script, app.name, app.proxy.proxy)
    // 生命周期挂载
    if (isCheckLifecycle(lifecycle)) {
        app.bootstrap = lifecycle.bootstrap
        app.mounted = lifecycle.mounted
        app.unmount = lifecycle.unmount
    }
}
